<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item>书籍机翻</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @selection-change="selectionChange" 
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-save="rowSave"
      >
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button v-if="scope.row.state==0 && (scope.row.book_id_to==undefined||scope.row.book_id_to=='')"
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="deleteTask(scope.row)"
            >  移除任务 </el-button>
          <el-button v-if="scope.row.state==0 && scope.row.is_top==0"
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="topTask(scope.row)"
            >  任务置顶 </el-button>
          <el-button v-if="scope.row.state==4"
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="againTask(scope.row)"
            >  继续翻译 </el-button>
        </template>
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            plain
            @click.stop="deleteCheck()"
            >移除任务</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click.stop="handleExcel()"
            >列表导出</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
    <export-message :dialogVisible="dialogVisible" ></export-message>
  </div>
</template>

<script>
import ExportMessage from "@/components/export/message";
import { common } from "@/api/cat/httpRequest";
import { translation as baseURL } from "@/api/cat/baseUrls";
export default {
  components: { ExportMessage },
  data(){
    return{
      dialogVisible: false,
      loading:false,
      page: {
          total: 0,
          pageSize: 10
      },
      checks:[],
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
      },
      option: {
          addBtn: true,
          addBtnText: "添加任务",
          editBtn: false,
          viewBtn: false,
          delBtn: false,
          excelBtn: false,
          border: true,
          menu: true,
          selection: true,
          column: [
              {
                  label: "搜索日期",
                  prop: "statistics_date_search",
                  type: "datetimerange",
                  search: true,
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  hide:true
              },
              {
                  label: "创建时间",
                  prop: "create_time",
                  type: "date",
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  width: "150px"
              },
              {
                  label: "书籍id from",
                  prop: "book_id",
                  search: true,
                  span: 18,
                  type: "textarea",
                  placeholder: "英文逗号拼接",
                  width: "160px"
              },
              {
                  label: "书籍id to",
                  prop: "book_id_to",
                  search: true,
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  type: "input",
                  width: "160px"
              },
              {
                  label: "书籍名称from",
                  prop: "book_name",
                  type: "input",
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  width: "300px"
              },
              {
                  label: "书籍名称to",
                  prop: "book_name_to",
                  type: "input",
                  width: "300px",
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  hide: true
              },
              {
                  label: "语言from",
                  prop: "book_language",
                  type: "tree",
                  width: "130px",
                  search: true,
                  dicData: [
                    {
                      label: "自动检测",
                      value: 0,
                    },
                    {
                      label: "中文(简体)",
                      value: 1,
                    },
                    {
                      label: "中文(繁体)",
                      value: 4,
                    },
                    {
                      label: "中文(粤语)",
                      value: 5,
                    },
                    {
                      label: "中文(文言文)",
                      value: 6,
                    },
                    {
                      label: "英语",
                      value: 2,
                    },
                    {
                      label: "芬兰语",
                      value: 3,
                    },
                    {
                      label: "日语",
                      value: 7,
                    },
                    {
                      label: "韩语",
                      value: 8,
                    },
                    {
                      label: "法语",
                      value: 9,
                    },
                    {
                      label: "西班牙语",
                      value: 10,
                    },
                    {
                      label: "泰语",
                      value: 11,
                    },
                    {
                      label: "阿拉伯语",
                      value: 12,
                    },
                    {
                      label: "俄语",
                      value: 13,
                    },
                    {
                      label: "葡萄牙语",
                      value: 14,
                    },
                    {
                      label: "德语",
                      value: 15,
                    },
                    {
                      label: "意大利语",
                      value: 16,
                    },
                    {
                      label: "希腊语",
                      value: 17,
                    },
                    {
                      label: "荷兰语",
                      value: 18,
                    },
                    {
                      label: "波兰语",
                      value: 19,
                    },
                    {
                      label: "保加利亚语",
                      value: 20,
                    },
                    {
                      label: "爱沙尼亚语",
                      value: 21,
                    },
                    {
                      label: "丹麦语",
                      value: 22,
                    },
                    {
                      label: "捷克语",
                      value: 23,
                    },
                    {
                      label: "罗马尼亚语",
                      value: 24,
                    },
                    {
                      label: "斯洛文尼亚语",
                      value: 25,
                    },
                    {
                      label: "瑞典语",
                      value: 26,
                    },
                    {
                      label: "匈牙利语",
                      value: 27,
                    },
                    {
                      label: "越南语",
                      value: 28,
                    },
                    {
                      label: "世界语",
                      value: 29,
                    }
                  ],
              },
              {
                  label: "语言to",
                  prop: "book_language_to",
                  type: "tree",
                  width: "130px",
                  search: true,
                  dicData: [
                    {
                      label: "自动检测",
                      value: 0,
                    },
                    {
                      label: "中文(简体)",
                      value: 1,
                    },
                    {
                      label: "中文(繁体)",
                      value: 4,
                    },
                    {
                      label: "中文(粤语)",
                      value: 5,
                    },
                    {
                      label: "中文(文言文)",
                      value: 6,
                    },
                    {
                      label: "英语",
                      value: 2,
                    },
                    {
                      label: "芬兰语",
                      value: 3,
                    },
                    {
                      label: "日语",
                      value: 7,
                    },
                    {
                      label: "韩语",
                      value: 8,
                    },
                    {
                      label: "法语",
                      value: 9,
                    },
                    {
                      label: "西班牙语",
                      value: 10,
                    },
                    {
                      label: "泰语",
                      value: 11,
                    },
                    {
                      label: "阿拉伯语",
                      value: 12,
                    },
                    {
                      label: "俄语",
                      value: 13,
                    },
                    {
                      label: "葡萄牙语",
                      value: 14,
                    },
                    {
                      label: "德语",
                      value: 15,
                    },
                    {
                      label: "意大利语",
                      value: 16,
                    },
                    {
                      label: "希腊语",
                      value: 17,
                    },
                    {
                      label: "荷兰语",
                      value: 18,
                    },
                    {
                      label: "波兰语",
                      value: 19,
                    },
                    {
                      label: "保加利亚语",
                      value: 20,
                    },
                    {
                      label: "爱沙尼亚语",
                      value: 21,
                    },
                    {
                      label: "丹麦语",
                      value: 22,
                    },
                    {
                      label: "捷克语",
                      value: 23,
                    },
                    {
                      label: "罗马尼亚语",
                      value: 24,
                    },
                    {
                      label: "斯洛文尼亚语",
                      value: 25,
                    },
                    {
                      label: "瑞典语",
                      value: 26,
                    },
                    {
                      label: "匈牙利语",
                      value: 27,
                    },
                    {
                      label: "越南语",
                      value: 28,
                    },
                    {
                      label: "世界语",
                      value: 29,
                    }
                  ],
              },
              {
                  label: "当前翻译进度",
                  prop: "state",
                  type: "select",
                  width: "130px",
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  search: true,
                  dicData: [
                      {
                          label: "队列中",
                          value: 0,
                      },
                      {
                          label: "书籍基本信息翻译中",
                          value: 1,
                      },
                      {
                          label: "章节信息翻译中",
                          value: 2,
                      },
                      {
                          label: "全书完成",
                          value: 3,
                      },
                      {
                          label: "翻译失败",
                          value: 4,
                      },
                  ],
              },
              {
                  label: "完成时间",
                  prop: "completion_time",
                  type: "date",
                  addDisplay: false,
                  editDisplay: false,
                  addDisabled: true,
                  editDisabled: true,
                  width: "150px"
              }
          ]
      }
    }
  },
  methods:{
    /**勾选所有 */
    selectionChange (row) {
      this.checks = row
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      this.dialogVisible = false;
        //清空表格列表
        this.data = [];
        //发送请求获取数据
        let response = await common(
            baseURL.page,
            { ...this.params, ...this.default },
            this.page,
            "POST"
        );
        this.page = response.page;
        this.data = response.data;
        this.loading = false;
    },

    /**导出 */
    async handleExcel(){
      this.loading = true;
      this.dialogVisible = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm};
      
      params.route = "内容管理/书籍机翻";
      
      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.user_id = userInfo.id;
      params.user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000;
      if(params.statistics_date_search.length>0){
        export_condition += " &时间区间："+params.statistics_date_search[0].toLocaleDateString()+"~"+params.statistics_date_search[1].toLocaleDateString()
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }
      if(params.state){
        export_condition += " &当前翻译进度："+this.option.column[8].dicData.filter(source => source.value == params.state)[0].label;
      }
      if(params.book_id){
        export_condition += " &书籍id from："+params.book_id;
      }
      if(params.book_id_to){
        export_condition += " &书籍id to："+params.book_id_to;
      }
      if(params.book_language){
        export_condition += " &语言from："+this.option.column[7].dicData.filter(source => source.value == params.book_language)[0].label;
      }
      if(params.book_language_to){
        export_condition += " &语言to："+this.option.column[8].dicData.filter(source => source.value == params.book_language_to)[0].label;
      }

      params.export_condition = export_condition == "" ? "所有" : export_condition;
      
      //调用导出接口
      let response = await common(
        baseURL.exportFile,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },

    /**保存 */
    async rowSave(row, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.user_name = userInfo.username;
      row.user_id = userInfo.id;
      let data = await common(baseURL.add, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },

    /**移除任务 */
    deleteTask(row){
      if(row.state==0 && (row.book_id_to==undefined||row.book_id_to=='')){
        return this.del([row.id]);
      }
      this.$message.info("不可移除非队列中任务或继续翻译任务")
    },

    /**移除任务-批量移除 */
    deleteCheck(){
      let length = this.checks.filter(item => item.state != 0).length;
      if(length>0){
        return this.$message.info("不可移除非队列中任务");
      }
      length = this.checks.filter(item => item.state == 0 && (item.book_id_to==undefined || item.book_id_to=='')).length;
      if(length != this.checks.length){
        return this.$message.info("不可移除继续翻译任务");
      }
      if(this.checks.length==0){
        return this.$message.info("请选择要移除的队列中任务")
      }
      let arr = this.checks.map(item => item.id);
      this.del(arr);
    },

    /**任务置顶 */
    async topTask(row){
      if(row.state==0){
        let data = await common(`${baseURL.top}${row.id}`, null, null, "PUT");
        this.refreshChange();
        return;
      }
      this.$message.info("不可置顶非队列中任务")
    },

    /**重新翻译 */
    async againTask(row){
      if(row.state==4){
        let data = await common(`${baseURL.again}/${row.id}`, null, null, "PUT");
        this.refreshChange();
        return;
      }
      this.$message.info("不可继续翻译非失败任务")
    },

    del(ids){
      this.$confirm(
        "确定要移除吗？",
        "提示",
        {
          type: "warning",
        }
      )
      .then(async () => {
        let data = await common(`${baseURL.del}`, ids, null, "DELETE");
        if (data == null) {
          return
        }
        this.refreshChange();
      })
      .catch(() => {
        this.$message({
          type: "info",
          message: "已取消移除",
        });
      });
    },
    
    /**刷新 */
    refreshChange() {
      this.getList();
    },

    /**搜索 */
    searchChange(params, done) {
      if(params.statistics_date_search){
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }

      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },

    /**请求是否成功 */
    isSuccess(code){
      return code == 1;
    }
  }
}
</script>

<style>

</style>